package iotwisp.base.tree;

import javax.persistence.Column;
import javax.persistence.Embeddable;

import lombok.Data;

@Data
@Embeddable
public class EMTreeData implements TreeNode {

	@Column(name = "treeId", length = 64)
	private String treeId;

	@Column(name = "parent_id", length = 64)
	private String parentId;

	@Column(name = "left_value", length = 8)
	private Integer leftValue;

	@Column(name = "right_value", length = 8)
	private Integer rightValue;

	@Column(name = "level", length = 8)
	private Integer level;

	public Integer getWidth() {
		return getRightValue() - getLeftValue() + 1;
	}

	public EMTreeData() {
		super();
	}

	public EMTreeData(String treeId, boolean root) {
		setTreeId(treeId);
		if (root) {
			setLeftValue(1);
			setRightValue(2);
			setLevel(0);
		}
	}

	public EMTreeData(String parentId, EMTreeData parentTreeData) {
		setTreeId(parentTreeData.getTreeId());
		setParentId(parentId);
		setLevel(parentTreeData.getLevel() + 1);
		setLeftValue(parentTreeData.getRightValue());
		setRightValue(parentTreeData.getRightValue() + 1);
	}
	
	public EMTreeData(String treeId, String parentId, Integer level, Integer lastValue) {
		setTreeId(treeId);
		setParentId(parentId);
		setLevel(level);
		setLeftValue(lastValue + 1);
		setRightValue(lastValue + 2);
	}

	public boolean isUpNode(TreeDataBaseEntity targetParentOrgType) {

		if (getLeftValue() < targetParentOrgType.getTreeData().getLeftValue()
				&& getRightValue() > targetParentOrgType.getTreeData().getRightValue()) {
			return true;
		} else {
			return false;
		}

	}
}
